<template>
	<view class="wrap">
		<u-form :model="model" :rules="rules" ref="uForm">
			<u-form-item  label-width="350"
				:label-position="labelPosition" label="内径(mm)" prop="innerDiameter">
				<u-input :border="border" placeholder="请输入内径" v-model="model.innerDiameter" type="digit"></u-input>
			</u-form-item>
			<u-form-item  label-width="350"
				:label-position="labelPosition" label="压力(KN)" prop="pressure">
				<u-input :border="border" placeholder="请输入压力" v-model="model.pressure" type="digit"></u-input>
			</u-form-item>
		</u-form>

		<u-button type="primary" @click="submit">计算</u-button>

		<view class="u-config-wrap">
			<view class="u-config-item">
				<view class="u-item-title">计算结果</view>
				<u-form-item style="font-size: 22px;"  :leftIconStyle="{color: '#888', fontSize: '32rpx'}" label-width="300"
					:label-position="labelPosition" label="主应力" prop="principalStress">
					<span v-if="model.principalStress"> {{model.principalStress}} MPa </span>
				</u-form-item>
			</view>
			<view class="u-config-item">
				<view class="u-item-title">计算公式</view>
				<view class="u-item-desc">主应力 = √(内径² + 4 × 压力²)</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				model: {
					innerDiameter: '',
					pressure: '',
					principalStress: ''
				},
				rules: {
					innerDiameter: [{
						required: true,
						message: '请输入内径',
						trigger: 'blur'
					}],
					pressure: [{
						required: true,
						message: '请输入压力',
						trigger: 'blur'
					}]
				},
				labelPosition: 'left',
				border: true,
				
			};
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						const d = parseFloat(this.model.innerDiameter);
						const P = parseFloat(this.model.pressure);
						
						// 主应力 = √(内径² + 4 × 压力²)
						this.model.principalStress = Math.sqrt(Math.pow(d, 2) + 4 * Math.pow(P, 2)).toFixed(2);
					}
				});
			}
		}
	};
</script>

<style scoped lang="scss">
	.wrap {
		padding: 30rpx;
	}

	.u-config-wrap {
		margin-top: 30rpx;
	}

	.u-config-title {
		padding: 20rpx;
		font-size: 32rpx;
		font-weight: bold;
	}

	.u-config-item {
		padding: 20rpx;
	}

	.u-item-title {
		font-size: 28rpx;
		font-weight: bold;
		margin-bottom: 10rpx;
	}

	.u-item-desc {
		font-size: 24rpx;
		color: #666;
	}
</style>